//
//  CZMainViewController.swift
//  Weibo27
//
//  Created by apple on 16/4/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 初始化tabbar
        let tabbar = CZTabBar()
        // 监听撰写按钮点击
        //        tabbar.composeButton.addTarget(self, action: "", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 
        tabbar.czDeletate = self
        // self.tabBar  系统私有属性 不能直接设置
        // 通过kvc
        setValue(tabbar, forKey: "tabBar")
        
        addChildViewController(CZHomeViewController(), imgName: "tabbar_home", title: "首页")
        addChildViewController(CZMessageViewController(), imgName: "tabbar_message_center", title: "消息")
        addChildViewController(CZDiscoverViewController(), imgName: "tabbar_discover", title: "发现")
        addChildViewController(CZProfileViewController(), imgName: "tabbar_profile", title: "我的")
    

    }
    
    // 添加子控制器方法
    func addChildViewController(childController: UIViewController, imgName: String, title: String) {

        // 设置图片
        childController.tabBarItem.image = UIImage(named: imgName)
        // 设置选中图片
        childController.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected");
        // 设置title
        childController.title = title
        // 添加子控制器
        addChildViewController(UINavigationController(rootViewController: childController))

    }

}
// MARK: - CZTabBarDelegate
// extension 相当于我们OC中的分类 extension 后面的类 CZMainViewController 就代表是当前类的分类
extension CZMainViewController: CZTabBarDelegate{
    func composeButtonClick() {
        let composeVc = CZComposeViewController()
        presentViewController(UINavigationController(rootViewController: composeVc), animated: true, completion: nil)
    }
}
